package ro.cityprojects.adminloc.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import ro.cityprojects.adminloc.dao.BasicRepository;
import ro.cityprojects.adminloc.entities.Association;
import ro.cityprojects.adminloc.entities.Street;
import ro.cityprojects.adminloc.exceptions.EntityNotFoundException;
import ro.cityprojects.adminloc.exceptions.ValidationException;
import ro.cityprojects.adminloc.metadata.EntityMetadata;

import javax.validation.Valid;
import java.util.Set;

/**
 * AssociationService class
 *
 * @author bogdan.solga
 *
 * Date: 18.06.2013, time: 19:45
 */
@Service
public class AssociationServiceImpl extends BaseService implements AssociationService {

    @Autowired
    private BasicRepository<Integer, Association> repository;

    @Autowired
    private BasicRepository<Integer, Street> streetsRepository;

    @Override
    public String[] getDefaultFields() {
        return new String[]{"id", "name", "postalCode"};
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(@Valid Association association) throws ValidationException {
        //TODO: add a field in the UI for the streetID
        association.setStreetByStreetId(streetsRepository.get(Street.class, 1));

        repository.save(association);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String get(Integer primaryKey, String includedFields) throws EntityNotFoundException {
        Association association = repository.get(Association.class, primaryKey);

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(association, outputFields);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String getAll(String filterName, String filterValue, String includedFields) throws EntityNotFoundException {
        Set<Association> all = repository.getAll(Association.class, filterName, filterValue);
        Assert.notEmpty(all, "There are no Associations with the specified details");

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(all, outputFields);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(Association association, Integer primaryKey) throws ValidationException {
        Association updated = repository.get(Association.class, primaryKey);

        BeanUtils.copyProperties(association, updated);

        //TODO: add a field in the UI for the streetID
        updated.setStreetByStreetId(streetsRepository.get(Street.class, 2));

        repository.update(updated);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(Integer primaryKey) throws EntityNotFoundException {
        repository.delete(Association.class, primaryKey);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public void checkExisting(Association association) throws ValidationException {
        String name = association.getName();
        Association existing = repository.getByProperty(Association.class, "name", name);
        if (existing != null) {
            throw new ValidationException("Exista deja o asociatie cu numele '" + name + "'");
        }
    }

    @Override
    public String getMetadata() {
        return getMetadata(EntityMetadata.Association);
    }
}
